//
//  KKAdviceSortDialog.m
//  DoMi
//
//  Created by 康凯 on 2016/12/5.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKAdviceSortDialog.h"
#import "KKStudyItem.h"
#import "KKQuestionItemCell.h"
#import "KKAdviceItemM.h"

@interface KKAdviceSortDialog()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)NSMutableArray *shaiXuanItems;
@property(nonatomic,strong)UIButton *currentRadio1;
@property(nonatomic,strong)UIButton *currentRadio2;
@property(nonatomic,strong)NSArray *items;
@property(nonatomic,strong)NSMutableString *types;
@property(nonatomic,strong)NSString *type;
@end

@implementation KKAdviceSortDialog

-(NSMutableString *)types{
    if(!_types){
        _types=[NSMutableString string];
    }
    return _types;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    [self initData];
}

-(void)initData:(NSArray *)items{
    self.items=items;
}

-(void)initData{
    self.frame = [UIScreen mainScreen].bounds;
    self.containerBottom.constant=-440;
    [self layoutIfNeeded];
    self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    //初始化数据
//    默认不传,传多个值时用逗号隔开）、排序（评分高到底:judgerank_up、评分低到高:judgerank_down、费用高到底:money_up、费用倒叙:money_down）
    NSArray *titles=@[@"好评由高到低",@"好评由低到高",@"费用由高到低",@"费用由低到高"];
    NSArray *codes=@[@"judgerank_up",@"judgerank_down",@"money_up",@"money_down"];
    self.shaiXuanItems=[NSMutableArray array];
    for (int i=0;i<titles.count;i++) {
        KKStudyItem *item=[[KKStudyItem alloc] init];
        item.title=titles[i];
        item.code=codes[i];
        [self.shaiXuanItems addObject:item];
    }
    
    //确定是水平滚动，还是垂直滚动
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.itemSize=CGSizeMake((screenWidth-20)/2, 35);
    flowLayout.minimumLineSpacing=1;
    flowLayout.minimumInteritemSpacing=1;
    
    UICollectionViewFlowLayout *flowLayout1=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout1 setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout1.itemSize=CGSizeMake((screenWidth-20)/2, 35);
    flowLayout1.minimumLineSpacing=1;
    flowLayout1.minimumInteritemSpacing=1;
    
    //领域
    self.collectionView1.collectionViewLayout=flowLayout;
    self.collectionView1.dataSource=self;
    self.collectionView1.delegate=self;
    self.collectionView1.backgroundColor=[UIColor colorWithHexString:@"#E3E3E0"];
    self.collectionView1.layer.cornerRadius=4;
    //注册Cell
    [self.collectionView1 registerNib:[UINib nibWithNibName:@"KKQuestionItemCell" bundle:nil] forCellWithReuseIdentifier:@"KKQuestionItemCell"];
    self.collectionView1.tag=100;
    
    //其他
    self.collectionView2.collectionViewLayout=flowLayout1;
    self.collectionView2.dataSource=self;
    self.collectionView2.delegate=self;
    self.collectionView2.backgroundColor=[UIColor colorWithHexString:@"#E3E3E0"];
    self.collectionView2.layer.cornerRadius=4;
    //注册Cell
    [self.collectionView2 registerNib:[UINib nibWithNibName:@"KKQuestionItemCell" bundle:nil] forCellWithReuseIdentifier:@"KKQuestionItemCell"];
    self.collectionView2.tag=101;
    
    self.type=@"";
}

#pragma mark collectionview代理
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(collectionView.tag==100){
        return self.items.count;
    }else{
        return self.shaiXuanItems.count;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KKQuestionItemCell *cell = (KKQuestionItemCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"KKQuestionItemCell" forIndexPath:indexPath];
    if(collectionView.tag==100){
        KKAdviceItemM *item=self.items[indexPath.row];
        [cell initTitleData:&item];
    }else{
        KKStudyItem *item=self.shaiXuanItems[indexPath.row];
        [cell initData:&item];
    }
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((screenWidth-15)/2, 35);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if(section==0){
        return UIEdgeInsetsMake(2, 2, 0, 2);
    }
    return UIEdgeInsetsMake(0, 2, 0, 2);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(collectionView.tag==100){
        KKAdviceItemM *item= self.items[indexPath.row];
        if(self.currentRadio1!=item.radio){
            self.currentRadio1=item.radio;
            item.radio.selected=YES;
            if(![self.types containsString:item.param1]){
                [self.types appendFormat:@",%@",item.param1];
            }
        }
    }else{
        KKStudyItem *item= self.shaiXuanItems[indexPath.row];
        if(self.currentRadio2!=item.radio){
            self.currentRadio2.selected=NO;
            item.radio.selected=YES;
            self.currentRadio2=item.radio;
            self.type=item.code;
        }
    }
}

#pragma mark 弹框--展示
-(void)show{
    [appWindow addSubview:self];
    [UIView animateWithDuration:dialog_animation_time animations:^{
        self.containerBottom.constant=0;
        [self layoutIfNeeded];
    }];
}
#pragma marm 弹框--消失
-(void)dismiss{
    [UIView animateWithDuration:dialog_animation_time animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        self.containerBottom.constant=-440;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (IBAction)ok:(id)sender {
    if([KKAppUtils isEmptyString:self.types]&&[KKAppUtils isEmptyString:self.type]){
        [KKAppUtils toast:@"请选择条目"];
        return;
    }
    [self dismiss];
    if(![KKAppUtils isEmptyString:self.types]){
        [self.types deleteCharactersInRange:NSMakeRange(0, 1)];
    }
    self.itemClickBlock(self.types,self.type);
}
#pragma mark 按钮--取消
- (IBAction)cancle:(id)sender {
    [self dismiss];
}


@end
